//package com.travel.travelServer.service1.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
//import com.travel.travelServer.entity.LoginUser;
//import com.travel.travelServer.entity.Role;
//import com.travel.travelServer.entity.User;
//import com.travel.travelServer.mapper.RoleMapper;
//import com.travel.travelServer.mapper.UserMapper;
//import com.travel.travelServer.service.UserService;
//import com.travel.travelServer.utils.AjaxResult;
//import com.travel.travelServer.utils.JWTUtils;
//import com.travel.travelServer.utils.RedisUtil;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
////import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Objects;
//
//@Service
//public class LoginServiceImpl implements LoginService {
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private UserMapper userMapper;
//
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//    @Resource
//    private AuthenticationManager authenticationManager;
//
//    @Autowired
//    private UserRoleMapper userRoleMapper;
//
//    @Autowired
//    private RoleMapper roleMapper;
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Override
//    public AjaxResult login(User user) {
//        boolean isNext = false;
//        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
//        userQueryWrapper.eq(User::getUsername, user.getUsername());
//        User dbUser = userService.getOne(userQueryWrapper);
//        if (Objects.isNull(dbUser)){
//            return AjaxResult.error("用户名不存在");
//        }else {
//            isNext = passwordEncoder.matches(user.getPassword(), dbUser.getPassword());
//        }
//        if (isNext){
//            UsernamePasswordAuthenticationToken authenticationToken =
//                    new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
//            Authentication authenticate = authenticationManager.authenticate(authenticationToken);
//            if (Objects.isNull(authenticate)){
//                // 未通过
//                return AjaxResult.error("登录失败");
//            }
//            // 通过
//            LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
//            Long id = loginUser.getUser().getId();
//            // 生成token
//            String token = JWTUtils.createToken(id.toString());
//            UserVo userVo = new UserVo();
//            User result = userService.getById(id);
//            BeanUtils.copyProperties(result, userVo);
//            LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(UserRole::getUserId, result.getId());
//            List<UserRole> userRoles = userRoleMapper.selectList(wrapper);
//            List<Role> roles = new ArrayList<>();
//            for (UserRole userRole : userRoles) {
//                Role role = roleMapper.selectById(userRole.getRoleId());
//                roles.add(role);
//            }
//            userVo.setRoles(roles);
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("token", token);
//            map.put("user", userVo);
//            // 将登录对象存入redis
//            redisUtil.set("login:" + id, loginUser);
//            return AjaxResult.success(map);
//        }else {
//            return AjaxResult.error("密码错误");
//        }
//    }
//
//    @Override
//    public AjaxResult reg(User user){
//        User dbUser = userService.selectByUsername(user.getUsername());
//        if (Objects.nonNull(dbUser)){
//            return AjaxResult.error("该用户名已被注册");
//        }
//        String inputPassword = user.getPassword();
//        String encodePassword = passwordEncoder.encode(inputPassword);
//        user.setPassword(encodePassword);
//        UserVo userVo = new UserVo();
//        BeanUtils.copyProperties(user, userVo);
//        return AjaxResult.success(SqlHelper.retBool(userService.insertUser(userVo)));
//    }
//
//    @Override
//    public Boolean quit(User user) {
//        redisUtil.del(user.getUsername());
//        return true;
//    }
//}
